# -*- coding: utf-8 -*-
import sys
import csv
from nltk.metrics.distance import masi_distance

CSV_FILE = 'C:\Users\dauster\Desktop\FG.MIneracao de dados\linkedln\linkedin_connections_export_microsoft_outlook.csv'
DISTANCE_THRESHOLD = 0.34
DISTANCE = masi_distance

def cluster_contatos_by_cargo(csv_file):

    # complementos encontrados em alguns cargos a qual substituiremos para nossa aplicação
    transforms = [
           ('Pleno'    , ''),
           ('Senior'   , ''),
           ('de'       , ''),
           ('Suporte'  , 'Piao Nivel 1'),
           ('Gerente.' , 'Chefe'),
           ('Diretor'  , 'Dono'),
           ('Analista' , 'Piao Nivel 2'),
           ('Consultor', 'Autonomo'),
            ]
    seperators = ['/', 'and', '&']

    csvReader = csv.DictReader(open(csv_file), delimiter=',', quotechar='"')
    contatos = [row for row in csvReader]

 # Normaliza e  substituir abreviaturas conhecidas
 # E construir a lista de cargos comuns

    all_cargos = []
    for i in range(len(contatos)):
        if contatos[i]['Job Title'] == '':
            contatos[i]['Job Titles'] = ['']
            continue
        cargos = [contatos[i]['Job Title']]
        for cargo in cargos:
            for seperator in seperators:
                if cargo.find(seperator) >= 0:
                    cargos.remove(cargo)
                    cargos.extend([cargo.strip() for cargo in cargo.split(seperator)
                                  if cargo.strip() != ''])

        for transform in transforms:
            cargos = [cargo.replace(*transform) for cargo in cargos]
        contatos[i]['Job Title'] = cargos
        all_cargos.extend(cargos)

    all_cargos = list(set(all_cargos))

    clusters = {}
    for cargo1 in all_cargos:
        clusters[cargo1] = []
        for cargo2 in all_cargos:
            if cargo2 in clusters[cargo1] or clusters.has_key(cargo2) and cargo1 \
                in clusters[cargo2]:
                continue
            distance = DISTANCE(set(cargo1.split()), set(cargo2.split()))
            if distance < DISTANCE_THRESHOLD:
                clusters[cargo1].append(cargo2)

# Planifica os agrupamentos

    clusters = [clusters[cargo] for cargo in clusters if len(clusters[cargo]) > 1]

# Reune os contatos que estão nesses agrupamentos, agrupanda-os

    clustered_contatos = {}
    for cluster in clusters:
        clustered_contatos[tuple(cluster)] = []
        for contato in contatos:
            for cargo in contato['Job Title']:
                if cargo in cluster:
                    clustered_contatos[tuple(cluster)].append('%s %s.'
                            % (contato['First Name'], contato['Last Name'][0]))

    return clustered_contatos

if __name__ == '__main__':
    clustered_contatos = cluster_contatos_by_cargo(CSV_FILE)

    for cargos in clustered_contatos:
        cargos_comuns_cabecalho = 'Cargos Comuns: ' + ', '.join(cargos)
        print cargos_comuns_cabecalho

        termos = set(cargos[0].split())
        for cargo in cargos:
            termos.intersection_update(set(cargo.split()))
        termos_cabecalho = 'Termos Discritivos: ' \
            + ', '.join(termos)
        print termos_cabecalho
        print '-' * max(len(termos_cabecalho), len(cargos_comuns_cabecalho))
        print '\n'.join(clustered_contatos[cargos])
        print
